<template>
  <div class="">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button class="keBtn" type="primary" @click="addrolesList">添加用户</el-button>
      <el-table :data="roleslist" style="width: 100%" stripe border>
        <el-table-column type="expand"></el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="editRolselist(scope.row.id)">编辑
            </el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="delteRolselist(scope.row.id)">删除
            </el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="修改用户" :visible.sync="rolesUserdialogVisible" width="30%" @close="resetRolesUsersDia">
      <el-form ref="rolesform" :model="rolesForm" label-width="80px" :rules="rloesUserRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="rolesForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="rolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rolesUserdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submenueRloesUser">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="添加用户" :visible.sync="aDDUserdialogVisible" width="30%" @close="resetRolesUsersDia">
      <el-form ref="rolesform" :model="rolesForm" label-width="80px" :rules="rloesUserRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="rolesForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="rolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="aDDUserdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submenueaddRloesUser">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="删除用户" :visible.sync="delteUserdialogVisible" width="30%" @close="resetRolesUsersDia">
      <el-form ref="rolesform" :model="rolesForm" label-width="80px" :rules="rloesUserRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="rolesForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="rolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delteUserdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submenuedelteRloesUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    components: {},
    data() {
      return {
        roleslist: [],
        rolesForm: {
          roleName: '',
          roleDesc: '',
          roleId: '',
        },
        rolesUserdialogVisible: false,
        aDDUserdialogVisible: false,
        delteUserdialogVisible: false,
        rloesUserRules: {
          roleName: [{
              required: true,
              message: '请输入用户名称',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 10,
              message: '用户名在2~10个字符之间',
              trigger: 'blur'
            }
          ],
          roleDesc: [{
              required: true,
              message: '请输入用户名称',
              trigger: 'blur'
            },
            {
              min: 3,
              max: 10,
              message: '用户描述在3~10个字符之间',
              trigger: 'blur'
            }
          ],
        }
      }
    },
    created() {
      this.rolesList()
    },
    mounted() {},
    watch: {},
    computed: {},
    methods: {
      rolesList() {
        this.$axios.get('roles').then(val => {
          console.log(val);
          if (val.meta.status != 200) {
            return this.$message.error('获取权限失败')
          }
          this.roleslist = val.data
        })
      },
      // 添加事件
      addrolesList() {
        this.aDDUserdialogVisible = true
      },
      submenueaddRloesUser() {
        this.$refs.rolesform.validate(vai => {
          if (!vai) {
            return
          }
          this.$axios.post('roles', this.rolesForm).then(val => {
            console.log(val);
            // 刷新列表
            this.rolesList()
            // 关闭窗口
            this.aDDUserdialogVisible = false
            //  弹窗提示
            this.$message.success("添加成功")
          })
        })
      },
      // 删除事件
      submenuedelteRloesUser() {
        this.$axios.delete('roles/'+this.rolesForm.roleId).then(val=>{
           if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg);
          }
          this.$message.error(val.meta.msg);
          
        })
         this.delteUserdialogVisible = false
      },
      delteRolselist(id) {
       this.$axios.get('roles/' + id).then(val => {
          if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg);
          }
          this.rolesForm.roleName = val.data.roleName;
          this.rolesForm.roleDesc = val.data.roleDesc;
          this.rolesForm.roleId = val.data.roleId;
          console.log(val.data.roleId);
        })
        this.delteUserdialogVisible = true
      },
      // 编辑事件
      editRolselist(id) {
        this.$axios.get('roles/' + id).then(val => {
          if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg);
          }
          this.rolesForm.roleName = val.data.roleName;
          this.rolesForm.roleDesc = val.data.roleDesc;
          this.rolesForm.roleId = val.data.roleId;
          console.log(val.data.roleId);
        })

        this.rolesUserdialogVisible = true
      },
      // 重置窗口
      resetRolesUsersDia() {
        this.$refs.rolesform.resetFields()
      },
      // 点击确定时间
      submenueRloesUser() {
        this.$refs.rolesform.validate(vai => {
          console.log(vai);
          if (!vai) {
            return
          };
          this.$axios.put('roles/' + this.rolesForm.roleId, {
            roleName: this.rolesForm.roleName,
            roleDesc: this.rolesForm.roleDesc,
          }).then(val => {
            if (val.meta.status != 200) {
              return this.$message.error(val.meta.msg)
            }
            this.$message.success("修改成功")
            this.rolesUserdialogVisible = false
            this.rolesList()
          })
        })
      }
    },
  }
</script>
<style lang="css" scoped>
</style>